﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Common;
using He.Web.UI;

namespace He.Web.manage.site
{
    public partial class config : He.Web.UI.Manage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                chkLoginLevel("editWebset");
                ShowInfo();
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            txtSiteDomain.Text = webset.site_domain;
            txtMobileUrl.Text = webset.mobile_url;

            if (!string.IsNullOrEmpty(webset.qrurl))
            {
                qr_review.Text = string.Format("<img src=\"{0}\" width=\"110\" />", webset.qrurl);
            }

            rblShowGuidePage.SelectedValue = webset.show_guide_page.ToString();
            rblShowBgMusic.SelectedValue = webset.show_bg_music.ToString();
            rblRightClickStatus.SelectedValue = webset.right_click_status.ToString();
            rblInnerDropdown.SelectedValue = webset.inner_dropdown.ToString();
            rblOlserviceStatus.SelectedValue = webset.olservice_status.ToString();
            rblShowFeedback.SelectedValue = webset.show_feedback.ToString().ToLower();

            rblMemberRegStatus.DataSource = EnumHelper.GetList(typeof(Enums.MemberStatus));
            rblMemberRegStatus.DataTextField = "Text";
            rblMemberRegStatus.DataValueField = "Value";
            rblMemberRegStatus.DataBind();
            rblMemberRegStatus.SelectedValue = webset.member_reg_status.ToString();

            txtEmailId.Text = webset.email_id;
            if (!string.IsNullOrEmpty(webset.email_pwd))
            {
                txtEmailPwd.Attributes["value"] = CryptoHelper.DESDecrypt(webset.email_pwd);
            }
            txtSmtpServer.Text = webset.email_smtpserver;
            txtRcKeyword.Text = webset.rc_keyword;
            txtHeadContent.Text = webset.head_content;
            txtBottomContent.Text = webset.bottom_content;
        }
        #endregion

        //保存配置
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //保存参数设置
            BLL.webset bll = new BLL.webset();
            Model.webset model = bll.GetWebset();

            model.site_domain = txtSiteDomain.Text.Trim().Trim('/');
            model.mobile_url = txtMobileUrl.Text.Trim().Trim('/');

            HttpPostedFile _posteQr = qrurl.PostedFile;
            if (_posteQr != null && _posteQr.FileName != string.Empty)
            {
                string _fileExt = _posteQr.FileName.Substring(_posteQr.FileName.LastIndexOf(".") + 1);
                //验证合法的文件
                if (!FileHelper.CheckFileExt(UploadImgExt, _fileExt))
                {
                    ShowError("不允许上传" + _fileExt + "类型的文件,请重试。");
                    return;
                }
                if (_posteQr.ContentLength > webset.upload_file_size * 1024)
                {
                    ShowError("图片超过限制，请先压缩图片。");
                    return;
                }
                FileHelper.DeleteFile(webset.qrurl);
                string _qrurl = "/images/sys/qr." + _fileExt;
                _posteQr.SaveAs(Server.MapPath(_qrurl));
                model.qrurl = _qrurl;
            }
            model.show_guide_page = int.Parse(rblShowGuidePage.SelectedValue);
            model.show_bg_music = int.Parse(rblShowBgMusic.SelectedValue);
            model.right_click_status = int.Parse(rblRightClickStatus.SelectedValue);
            model.inner_dropdown = int.Parse(rblInnerDropdown.SelectedValue);
            if (PlugHelper.IsShowPlug("olservice", lang))
            {
                model.olservice_status = int.Parse(rblOlserviceStatus.SelectedValue);
            }
            if (PlugHelper.IsShowPlug("feedback", lang))
            {
                model.show_feedback = bool.Parse(rblShowFeedback.SelectedValue);
            }
            if (PlugHelper.IsShowPlug("member", lang))
            {
                model.member_reg_status = int.Parse(rblMemberRegStatus.SelectedValue);
            }

            model.email_id = txtEmailId.Text.Trim();
            model.email_pwd = CryptoHelper.DESEncrypt(txtEmailPwd.Text.Trim());
            model.email_smtpserver = txtSmtpServer.Text.Trim();
            model.rc_keyword = txtRcKeyword.Text.Trim();
            model.head_content = txtHeadContent.Text;
            model.bottom_content = txtBottomContent.Text;

            bll.SaveConfig(model);
            RemoveAllCache();
            SaveLogs("[系统管理]网站参数设置");
            ShowSuccess("信息修改成功啦！", Request.RawUrl);
        }

        //删除二维码操作
        protected void lbtnDelQr_Click(object sender, EventArgs e)
        {
            FileHelper.DeleteFile(webset.qrurl);
            webset.qrurl = "";
            new BLL.webset().SaveConfig(webset);
            Response.Redirect(Request.RawUrl);
        }

        //删除手机网站logo操作
        protected void lbtnDelLogo_Click(object sender, EventArgs e)
        {
            FileHelper.DeleteFile(webset.logourl);
            webset.logourl = "";
            new BLL.webset().SaveConfig(webset); 
            Response.Redirect(Request.RawUrl);
        }
    }
}